package com.ytjj.common.api;

import com.ytjj.common.cmpp.SMProxyTools;
import com.ytjj.common.constants.QueueConstants;
import com.ytjj.common.dto.mqdto.SmsDto;
import com.ytjj.common.enums.ErrorMsgStatusEnum;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.AliyunSmsUtil;
import com.ytjj.common.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import static org.apache.commons.lang.RandomStringUtils.randomNumeric;

/**
 * @Description 通用发送消息
 *
 * @Version 1.0
 * 
 */

@Slf4j
public class SendMsg {
    public static Map<String, String> sendAuthCode(String telephone) {
        // 验证码
        String authCode = randomNumeric(6);
        HashMap<String, String> hashmap = new HashMap<>();
        hashmap.put("dc", "15");
        hashmap.put("sm", "【 XXXX优品】尊敬的用户，您的验证码是:" + authCode + "。请勿泄露给他人，如非本人操作，请忽略此短信。");
        hashmap.put("timestamp", LocalDateTime.now() + "");
        hashmap.put("da", telephone);
        hashmap.put("rd", "1");
        hashmap.put("un", "186900");
        hashmap.put("pw", "yt186900");
        hashmap.put("rf", "2");
        hashmap.put("tf", "3");
        int count = 0 ;
        String responseText = sendMsg(hashmap,count);
        log.info("responseText: {}", responseText);
        // 存储短信发送的内容
        HashMap<String, String> saveValue = new HashMap<>();
        saveValue.put("authCode", authCode);
        saveValue.put("sendMsg", hashmap.get("sm"));
        return saveValue;
    }

    public static Map<String, String> sendOrderInfo(String telephone) {
        // 验证码
        String authCode = randomNumeric(6);
//        HashMap<String, String> hashmap = new HashMap<>();
//        hashmap.put("dc", "15");
//        hashmap.put("sm", "【 XXXX优品】尊敬的用户：您购买的商品已下单成功，正在加急打包准备发货，请您注意收货。");
//        hashmap.put("timestamp", LocalDateTime.now() + "");
//        hashmap.put("da", telephone);
//        hashmap.put("rd", "1");
//        hashmap.put("un", "181494");
//        hashmap.put("pw", "yt181494");
//        hashmap.put("rf", "2");
//        hashmap.put("tf", "3");
//        int count = 0 ;
//        String responseText = sendMsg(hashmap,count);
//        log.info("responseText: {}", responseText);
        try{
//            AliyunSmsUtil.sendSms(telephone,1,"");
//            log.info("===cmpp下单成功发送短信手机号==={}", telephone);
//            String[] phones = {telephone};
//            SmsDto smsDto = new SmsDto();
//            smsDto.setPhone(telephone);
//            smsDto.setType(2);
//            rabbitTemplate.convertAndSend(QueueConstants.SMS_QUEUE,smsDto);
//            boolean result = SMProxyTools.sendMsg(phones, "【 XXXX优品】尊敬的用户，您购买的商品下单成功，正在为您加急打包准备发货，查询物流信息请下载< XXXX优品>APP，在我的订单中查看");
//            log.info("===cmpp下单成功发送短信结果===={}", result);
//            if (!result) {
//                throw new ApiException(ErrorMsgStatusEnum.SEND_ERROR.getMsg());
//            }
        }catch (Exception e) {
        }
        // 存储短信发送的内容
        HashMap<String, String> saveValue = new HashMap<>();
        saveValue.put("authCode", authCode);
        saveValue.put("sendMsg", "【XXXX优品】尊敬的用户：您购买的商品已下单成功，正在加急打包准备发货，请您注意收货。");
        return saveValue;
    }


    public static String sendOrderInfoMsg(String telephone, String url) {
        // 验证码
        //String authCode = randomNumeric(6);
        HashMap<String, String> hashmap = new HashMap<>();
        hashmap.put("dc", "15");
        hashmap.put("sm", "【XXXX优品】订单提醒：您有一个订单未付款成功，现可领取100元现金红包进行抵扣，请尽快点 " + url + " 进行支付。");
        hashmap.put("timestamp", LocalDateTime.now() + "");
        hashmap.put("da", telephone);
        hashmap.put("rd", "1");
        hashmap.put("un", "182504");
        hashmap.put("pw", "yt182504");
        hashmap.put("rf", "2");
        hashmap.put("tf", "3");
        int count = 0 ;
        String responseText = sendMsg(hashmap);
        log.info("responseText: {}", responseText);
        return responseText;
    }


    /**
     * 发送短信
     *
     * @param hashmap
     * @param count
     * @return
     */
    private static String sendMsg(HashMap<String, String> hashmap,Integer count){
        try {
            count++;
            return HttpUtil.post(
                    "http://61.129.57.244:7891/mt", hashmap);
        } catch (Exception e) {
            log.error("发送短信失败!!!");
            if(count < 3){
                return sendMsg(hashmap,count);
            }
            e.printStackTrace();
        }
        throw new ApiException("发送短信失败");
    }


    private static String sendMsg(HashMap<String, String> hashmap){
        try {
            String post =  HttpUtil.post(
                    "http://61.129.57.244:7891/mt", hashmap);
            log.info("结果：{}", post);
            return post;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送短信失败!!!");
        }
        return null;
    }

//    public static void main(String[] args) {
//        sendOrderInfo("18476643602");
//    }




}
